# -*-mode: Makefile;-*- Time-stamp: <2011-08-03 03:56:33 toge>

#CC  = ccache i586-mingw32-gcc
#CXX = ccache i586-mingw32-g++
#CC  = ccache gcc-4.4.0
#CXX = ccache g++-4.4.0
CC  = gcc-4.6
CXX = g++-4.6

#SDLCONFIG=/usr/local/i586-mingw32/bin/sdl-config
#SDLCONFIG=/usr/local/bin/sdl-config
SDLCONFIG=sdl-config

GCCVERSION:=$(shell $(CC) -dumpversion)
GCCMACHINE:=$(shell $(CC) -dumpmachine)

dot:=.
empty:=
space:=$(empty) $(empty)

## CFLAG, LDFLAGS
CFLAG  =
CXXFLAG =
LDFLAG =

# platform depend settings
ifeq ($(findstring mingw32,$(GCCMACHINE)),mingw32)
	EXEPREFIX:=.exe
  RANLIB:=i586-mingw32-ranlib
 else
	EXEPREFIX:=
	RANLIB:=ranlib
endif

# SDL/OpenGL
ifeq ($(findstring mingw32,$(GCCMACHINE)),mingw32)
	CFLAG  += $(shell $(SDLCONFIG) --cflags | sed "s/-I/-isystem /")
	LDFLAG +=  -lwinmm $(shell $(SDLCONFIG) --libs) -lpng -lz -lopengl32 -lwsock32 -lcomctl32 -lglu32
else
	SDL_CFLAGS:=
	CFLAG  += $(shell $(SDLCONFIG) --cflags | sed "s/-I/-isystem /")
	LDFLAG += -lGLU -lpng -lz -ldl $(shell $(SDLCONFIG) --libs) -lGL
endif

# local tricks
CFLAG  += -I. -Ifw -Igl2d
ifeq ($(findstring mingw32,$(GCCMACHINE)),mingw32)
	CFLAG  += -isystem ../boost_1_40_0
	CFLAG  += -isystem /c/MSYS/1.0/include -isystem /c/MSYS/1.0/local/include -isystem /usr/local/include/libpng
	LDFLAG += -L. -L/usr/lib
else
	CFLAG  += -isystem /home/toge/out/boost_1_51_0
endif

include gccoptions

DEPFILES =

all : lualib \
	ReflectBlade$(EXEPREFIX) \
	BoatRace$(EXEPREFIX) \
	VertexBench$(EXEPREFIX)\
	TextViewer$(EXEPREFIX)\
	IOTest$(EXEPREFIX)\

#================================================================================
LUAFILESYSTEM_SRC = $(wildcard lua/luafilesystem/src/*.c)
$(LUAFILESYSTEM_SRC:.c=.o) : %.o : %.c
	@$(CC) -O2 -Ilua/lua/src -c -o $@ $<

LUALIB_OBJ = $(LUAFILESYSTEM_SRC:.c=.o) lua/lua/src/liblua.a

LUATARGET =
ifeq ($(findstring mingw32,$(GCCMACHINE)),mingw32)
	LUATARGET = mingw
else ifeq ($(findstring linux,$(GCCMACHINE)),linux)
	LUATARGET = linux
else
	LUATARGET = ansi
endif


lualib : $(LUAFILESYSTEM_SRC:.cpp=.o)
	cd lua/lua; $(MAKE) CC="$(CC)" RANLIB="$(RANLIB)" $(LUATARGET)

#================================================================================
UGLY_FONT_SRC = $(wildcard uglyfont/*.cpp)
UGLY_FONT_OBJ = $(UGLY_FONT_SRC:.cpp=.o)

#================================================================================
GLEE_SRC =  $(wildcard glee/*.c)
GLEE_OBJ =  $(GLEE_SRC:.c=.o)

$(GLEE_OBJ) : %.o : %.c
	@$(CC) -c -O2 -o $@ $<

#================================================================================
SDL_GL2D_SRC = $(wildcard gl2d/*.c)
SDL_GL2D_OBJ = $(SDL_GL2D_SRC:.c=.o)
DEPFILES +=    $(SDL_GL2D_SRC:.c=.d)

#================================================================================
LIBLZF_SRC = $(wildcard liblzf/*.c)
LIBLZF_OBJ = $(LIBLZF_SRC:.c=.o)

$(LIBLZF_SRC:.c=.d) : %.d : %.c
	@$(CC) -O2 -Iliblzf -MM -MT $@ $<

#================================================================================
TINYLOG_SRC = $(wildcard tinylog/*.cpp)
TINYLOG_OBJ = $(TINYLOG_SRC:.cpp=.o)

#================================================================================
SHTTPD_SRC = $(wildcard shttpd/*.c)
SHTTPD_OBJ = $(SHTTPD_SRC:.c=.o)

$(SHTTPD_SRC:.c=.d) : %.d : %.c
	@$(CC) $(CFLAG) -MM -MT -DEMBEDDED $@ $<

$(SHTTPD_SRC:.c=.o) : %.o : %.c
	@$(CC) $(CFLAG) -DEMBEDDED -c -o $@ $<

#===============================================================================
FW_SRC = \
	fw/FrameClock.cpp     \
	fw/ObjectTail.cpp     \
	fw/Random.cpp         \
	fw/TextureManager.cpp \
	fw/Window.cpp         \
	fw/FPS.cpp            \
	fw/FPSView.cpp        \
	fw/Camera.cpp         \
	fw/CameraView.cpp     \
	fw/PostEffectView.cpp \
	fw/EffectViewer.cpp   \
	fw/GLUtil.cpp         \
	fw/ShaderGLSLBase.cpp \
	fw/Reader.cpp         \
	fw/PDRLoader.cpp      \
	fw/Effect.cpp         \
	fw/Slot.cpp           \
	fw/Button.cpp         \
	fw/Writer.cpp         \
	fw/ScreenCapture.cpp  \
	fw/TargetTexture.cpp  \
	fw/ObjectTailView.cpp \
	fw/LuaUtil.cpp        \

FW_OBJ    = $(FW_SRC:.cpp=.o)
DEPFILES += $(FW_SRC:.cpp=.d)

#================================================================================
REFLECT_SRC = $(wildcard reflect/*.cpp)
REFLECT_OBJ = $(REFLECT_SRC:.cpp=.o)
DEPFILES +=   $(REFLECT_SRC:.cpp=.d)

ReflectBlade$(EXEPREFIX) : $(REFLECT_OBJ) $(FW_OBJ) $(SDL_GL2D_OBJ) $(UGLY_FONT_OBJ) $(LUALIB_OBJ)  $(GLEE_OBJ) $(TINYLOG_OBJ) $(LIBLZF_OBJ)
	$(CXX) -o $@ $^ $(LDFLAG)

#================================================================================
VERTEX_BENCH_SRC = $(wildcard bench/*.cpp)
VERTEX_BENCH_OBJ = $(VERTEX_BENCH_SRC:.cpp=.o)
DEPFILES +=        $(VERTEX_BENCH_SRC:.cpp=.d)

VertexBench$(EXEPREFIX) : $(VERTEX_BENCH_OBJ) $(FW_OBJ) $(SDL_GL2D_OBJ) $(UGLY_FONT_OBJ) $(GLEE_OBJ) $(LUALIB_OBJ) $(TINYLOG_OBJ) $(LIBLZF_OBJ)
	@$(CXX) $(CFLAG) -o $@ $^ $(LDFLAG)

#================================================================================
BOATRACE_SRC             = $(wildcard boatrace/*.cpp)
BOATRACE_TITLE_SRC       = $(wildcard boatrace/title/*.cpp)
BOATRACE_STAGESELECT_SRC = $(wildcard boatrace/stageselect/*.cpp)
BOATRACE_RACE_SRC        = $(wildcard boatrace/race/*.cpp)

BOATRACE_OBJ = \
	$(BOATRACE_SRC:.cpp=.o) \
	$(BOATRACE_TITLE_SRC:.cpp=.o) \
	$(BOATRACE_STAGESELECT_SRC:.cpp=.o) \
	$(BOATRACE_RACE_SRC:.cpp=.o) \

DEPFILES += $(BOATRACE_OBJ:.o=.d)

$(BOATRACE_SRC:.cpp=.o) : %.o : %.cpp
	@$(CXX) $(CFLAG) -Iboatrace -c -o $@ $<
$(BOATRACE_SRC:.cpp=.d) : %.d : %.cpp
	@$(CXX) $(CFLAG) -Iboatrace -MM -MT $(<:.cpp=.o) $< >> DEPENDS

$(BOATRACE_TITLE_SRC:.cpp=.o) : %.o : %.cpp
#	mv $< $<.tmp; awk -f util/fiber.awk $<.tmp > $<; rm $<.tmp
	@$(CXX) $(CFLAG) -Iboatrace -Iboatrace/title -c -o $@ $<
$(BOATRACE_TITLE_SRC:.cpp=.d) : %.d : %.cpp
	@$(CXX) $(CFLAG) -Iboatrace -Iboatrace/title -MM -MT $(<:.cpp=.o) $< >> DEPENDS

$(BOATRACE_STAGESELECT_SRC:.cpp=.o) : %.o : %.cpp
	@$(CXX) $(CFLAG) -Iboatrace -Iboatrace/stageselect -c -o $@ $<
$(BOATRACE_STAGESELECT_SRC:.cpp=.d) : %.d : %.cpp
	@$(CXX) $(CFLAG) -Iboatrace -Iboatrace/stageselect -MM -MT $(<:.cpp=.o) $< >> DEPENDS

$(BOATRACE_RACE_SRC:.cpp=.o) : %.o : %.cpp
	@$(CXX) $(CFLAG) -Iboatrace -Iboatrace/race -c -o $@ $<
boatrace/race/ModelCollide.o : boatrace/race/ModelCollide.cpp
	@$(CXX) $(CFLAG) -fno-strict-aliasing -Iboatrace -Iboatrace/race -c -o $@ $<
$(BOATRACE_RACE_SRC:.cpp=.d) : %.d : %.cpp
	@$(CXX) $(CFLAG) -Iboatrace -Iboatrace/race -MM -MT $(<:.cpp=.o) $< >> DEPENDS

BoatRace$(EXEPREFIX) : $(BOATRACE_OBJ) $(FW_OBJ) $(SDL_GL2D_OBJ) $(UGLY_FONT_OBJ) $(TINYLOG_OBJ) $(LUALIB_OBJ) $(LIBLZF_OBJ) $(GLEE_OBJ) 
	@$(CXX) $(CFLAG) -o $@ $^ $(LDFLAG)

#================================================================================
TEXTVIEWER_SRC = $(wildcard textviewer/*.cpp)
TEXTVIEWER_OBJ = $(TEXTVIEWER_SRC:.cpp=.o)
DEPFILES +=  $(TEXTVIEWER_SRC:.cpp=.d)

TextViewer$(EXEPREFIX) : $(TEXTVIEWER_OBJ) $(FW_OBJ) $(SDL_GL2D_OBJ) $(UGLY_FONT_OBJ) $(TINYLOG_OBJ) $(GLEE_OBJ) $(LUALIB_OBJ) $(LIBLZF_OBJ)
	$(CXX) $(CFLAG) -o $@ $^ $(LDFLAG)

#================================================================================
IOTEST_SRC = $(wildcard iotest/*.cpp)
IOTEST_OBJ = $(IOTEST_SRC:.cpp=.o)
DEPFILES +=  $(IOTEST_SRC:.cpp=.d)

IOTest$(EXEPREFIX) : $(IOTEST_OBJ) $(FW_OBJ) $(SDL_GL2D_OBJ) $(UGLY_FONT_OBJ) $(TINYLOG_OBJ) $(LIBLZF_OBJ) $(LUALIB_OBJ) $(GLEE_OBJ)
	$(CXX) $(CFLAG) -o $@ $^ $(LDFLAG)

#================================================================================
.PHONY : all lualib clean updatedep

clean :
	cd lua/lua; CC="$(CC)" $(MAKE) clean
	cd Box2D/Source; $(MAKE) clean
	-@rm -f ReflectBlade$(EXEPREFIX) ReflectBlade2$(EXEPREFIX) VertexBench$(EXEPREFIX) BoatRace$(EXEPREFIX) TextViewer$(EXEPREFIX) IOTest$(EXEPREFIX) gmon.out stdout.txt stderr.txt $(REFLECT_OBJ) $(VERTEX_BENCH_OBJ) $(BOATRACE_OBJ) $(TEXTVIEWER_OBJ) $(FW_OBJ) $(PROGRAM_OPTION_OBJ) $(SDL_GL2D_OBJ) $(UGLY_FONT_OBJ) $(LIBLZF_OBJ) $(TINYLOG_OBJ) $(GLEE_OBJ) $(LUALIB_OBJ)

%.o: %.cpp
	@$(CXX) $(CFLAG) $(CXXFLAG) -c -o $@ $<

%.o: %.c
	@$(CC) $(CFLAG) -c -o $@ $<

%.d : %.cpp
	@$(CXX) $(CFLAG) $(CXXFLAG) -MM -MT $(<:.cpp=.o) $< >> DEPENDS

%.d : %.c
	@$(CC) $(CFLAG) -MM -MT $(<:.c=.o) $< >> DEPENDS

updatedep :
	-@rm DEPENDS
	make $(DEPFILES)

-include DEPENDS


